#!/usr/bin/env python3
"""
调试file.py模块的具体问题
"""

import sys
import os

# 添加项目根目录到Python路径
project_root = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, project_root)

print(f"项目根目录: {project_root}")
print(f"当前工作目录: {os.getcwd()}")

# 切换到项目目录
os.chdir(project_root)

try:
    print("1. 测试导入基础模块...")
    import sqlalchemy
    from sqlalchemy import Column, Integer, String, DateTime, JSON, Text, ForeignKey, BigInteger, Float
    from sqlalchemy.orm import relationship
    from datetime import datetime
    print("✅ 基础模块导入成功")
    
    print("2. 测试导入base模块...")
    from db.models.base import Base
    print("✅ base模块导入成功")
    
    print("3. 测试直接导入file模块...")
    # 直接导入file模块，不使用相对导入
    import importlib.util
    spec = importlib.util.spec_from_file_location("file_module", "db/models/file.py")
    file_module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(file_module)
    print("✅ file模块直接导入成功")
    
    print("4. 测试从file模块导入类...")
    FileUpload = file_module.FileUpload
    FileAnalysisResult = file_module.FileAnalysisResult
    print("✅ 类导入成功")
    
    print("5. 测试创建实例...")
    # 测试创建实例（不保存到数据库）
    print("✅ 所有测试通过")
    
except Exception as e:
    print(f"❌ 测试失败: {e}")
    import traceback
    traceback.print_exc()
    
    # 尝试更详细的错误分析
    print("\n=== 详细错误分析 ===")
    try:
        print("尝试逐行执行file.py...")
        with open("db/models/file.py", "r") as f:
            content = f.read()
            exec(content)
        print("✅ 逐行执行成功")
    except Exception as e2:
        print(f"❌ 逐行执行失败: {e2}")
        import traceback
        traceback.print_exc()
